package net.pluce.nxt.network;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;

/**
 * Interface defining a connection to a robot. It sends events to its listeners, and provides output and input streams to and from the robot.
 * Should be instanciated by a RobotConnectionFactory or you'll have to manually set an EventWatcher
 * @see net.pluce.nxt.network.RobotConnectionFactory
 * @author Pluce
 */
public interface RobotConnection {
	
	/**
	 * Add a listener to robot events
	 * @param listener
	 */
	public void addListener(RobotListener listener);
	
	/**
	 * Open a connection to the robot
	 * @return true if succeed, false otherwise
	 */
	public boolean open() throws RobotConnectionException;	
	
	/**
	 * Close the connection to the robot
	 * @return true if succeed, false otherwise
	 */
	public boolean close() throws RobotConnectionException;
	
	/**
	 * Check if the connection to the robot is opened.
	 * @return true if opened, false otherwise
	 */
	public boolean isOpened();
	
	/**
	 * Return a direct output stream to the robot
	 * @return stream to the robot
	 * @throws IllegalStateException thrown if connection closed
	 */
	public OutputStream getOutputStream() throws IllegalStateException;	
	
	/**
	 * Return a direct input stream from the robot
	 * @return stream from the robot
	 * @throws IllegalStateException thrown if connection closed
	 */
	public InputStream getInputStream()throws IllegalStateException;

	/**
	 * Return all listeners of a connection
	 * @return an unordered collection of listeners
	 */
	public Collection<RobotListener> getListeners();
	
	/**
	 * Sets the event watcher for the RobotConnection
	 * @param eventWatcher
	 */
	public void setEventWatcher(EventWatcher eventWatcher);
}
